package nc.tb.pub.table;

/*
 * @(#)src/classes/sov/javax/swing/DefaultCellEditor.java, as122, as122, 20000427 1.4.1.1
 * ===========================================================================
 * Licensed Materials - Property of IBM
 * IBM Java(tm)2 SDK, Standard Edition, v 1.2
 *
 * (C) Copyright IBM Corp. 1999 All Rights Reserved.
 * Copyright 1997-1999 by Sun Microsystems, Inc.,
 * ===========================================================================
 */
 
/*
 * @(#)DefaultCellEditor.java	1.34 99/04/22
 *
 */

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.util.EventObject;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;

import nc.tb.pub.ref.TBRefPane;
import nc.ui.pub.beans.UICheckBox;
import nc.ui.pub.beans.UIComboBox;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.beans.UITextField;

/**
 * The default editor for table and tree cells.
 * <p>
 * <strong>Warning:</strong>
 * Serialized objects of this class will not be compatible with 
 * future Swing releases.  The current serialization support is appropriate
 * for short term storage or RMI between applications running the same
 * version of Swing.  A future release of Swing will provide support for
 * long term persistence.
 *
 * @version 1.34 04/22/99
 * @author Philip Milne
 * @author Alan Chung
 */

public class DimCellEditor implements TableCellEditor, TreeCellEditor,
					  Serializable {
//
//  Instance Variables
//

	/** Event listeners */
	protected EventListenerList listenerList = new EventListenerList();
	transient protected ChangeEvent changeEvent = null;

	protected JComponent editorComponent;
	protected EditorDelegate delegate;
	protected int clickCountToStart = 1;

//
//  Protected EditorDelegate class
//

	protected class EditorDelegate implements ActionListener, ItemListener, Serializable {

		/** Not implemented. */
		protected Object value;

		/** Not implemented. */
		public Object getCellEditorValue() {
			return null;
		}

		/** Not implemented. */
		public void setValue(Object x) {}

		/** Not implemented. */
		public void setPK(Object x) {}
		
		/** Not implemented. */
		public boolean isCellEditable(EventObject anEvent) {
	    return true;
	}
		
		/** Unfortunately, restrictions on API changes force us to 
		  * declare this method package private. 
		  */
		boolean shouldSelectCell(EventObject anEvent) { 
			return true; 
		}


		/** Not implemented. */
		public boolean startCellEditing(EventObject anEvent) {
	    return true;
	}

		/** Not implemented. */
		public boolean stopCellEditing() {
	    return true;
	}

		/** Not implemented. */
	   public void cancelCellEditing() {
	   }

	// Implementing ActionListener interface
		public void actionPerformed(ActionEvent e) {
			fireEditingStopped();
	}

		// Implementing ItemListener interface
		public void itemStateChanged(ItemEvent e) {
	    fireEditingStopped();
	}
	}

/**
	 * Constructs a DefaultCellEditor object that uses a check box.
	 *
	 * @param x  a JCheckBox object ...
	 */
public DimCellEditor(final JCheckBox checkBox) {
	editorComponent = checkBox;
	delegate = new EditorDelegate() {
		public void setValue(Object value) {
			boolean selected = false;
			if (value instanceof Boolean) {
				selected = ((Boolean) value).booleanValue();
			} else
				if (value instanceof String) {
					selected = value.equals("true");
				}
			checkBox.setSelected(selected);
		}

		public Object getCellEditorValue() {
			return new Boolean(checkBox.isSelected());
		}
	};
	checkBox.addActionListener(delegate);
}
	/**
	 * Constructs a DefaultCellEditor object that uses a combo box.
	 *
	 * @param x  a JComboBox object ...
	 */
	public DimCellEditor(final JComboBox comboBox) {
		editorComponent = comboBox;
		delegate = new EditorDelegate() {
			public void setValue(Object value) {
		comboBox.setSelectedItem(value);
			}

	    public Object getCellEditorValue() {
		return comboBox.getSelectedItem();
	    }
				
			boolean shouldSelectCell(EventObject anEvent) { 
				if (anEvent instanceof MouseEvent) { 
					MouseEvent e = (MouseEvent)anEvent;
					return e.getID() != MouseEvent.MOUSE_DRAGGED;
				}
				return true;
			}
		};
	comboBox.addActionListener(delegate);
	}
//
//  Constructors
//

/**
 * Constructs a DefaultCellEditor that uses a text field.
 *
 * @param x  a JTextField object ...
 */
public DimCellEditor(final JTextField textField) {
    editorComponent = textField;
    this.clickCountToStart = 2;
    delegate = new EditorDelegate() {
        public void setValue(Object value) {
            textField.setText((value != null) ? value.toString() : "");
        }

        public Object getCellEditorValue() {
            return textField.getText();
        }
    };
    textField.addActionListener(delegate);
}
public DimCellEditor(final TBRefPane tbref) {
    editorComponent = tbref;
    final nc.ui.pub.beans.UITextField textField = tbref.getUITextField();
    //this.clickCountToStart = 2;
    delegate = new EditorDelegate() {
        public void setValue(Object value) {
            tbref.setValue((value != null) ? value.toString() : "");
            //textField.setText((value != null) ? value.toString() : "");
        }
        public Object getCellEditorValue() {
            //if (refTextField.isReturnCode()) //|| !refTextField.isButtonVisible())
                return tbref.getSelectedBusiObj();
            //return textField.getText();
            //else
                //return refTextField.getRefName();
        }
        public void setPK(Object value) {
            //refTextField.setPK((value != null) ? value.toString() : "");
        }
    };
    textField.addActionListener(delegate);
}
/**
	 * Constructs a DefaultCellEditor object that uses a check box.
	 *
	 * @param x  a JCheckBox object ...
	 */
public DimCellEditor(final UICheckBox checkBox) {
	editorComponent = checkBox;
	delegate = new EditorDelegate() {
		public void setValue(Object value) {
			boolean selected = false;
			if (value instanceof Boolean) {
				selected = ((Boolean) value).booleanValue();
			} else
				if (value instanceof String) {
					selected = value.equals("true");
				}else{
					if (value instanceof nc.vo.pub.lang.UFBoolean) {
						selected = ((nc.vo.pub.lang.UFBoolean)value).booleanValue();
					}
				}
			checkBox.setSelected(selected);
		}

		public Object getCellEditorValue() {
			return new Boolean(checkBox.isSelected());
		}
	};
	checkBox.addActionListener(delegate);
}
	/**
	 * Constructs a DefaultCellEditor object that uses a combo box.
	 *
	 * @param x  a JComboBox object ...
	 */
	public DimCellEditor(final UIComboBox comboBox) {
		editorComponent = comboBox;
		delegate = new EditorDelegate() {
			public void setValue(Object value) {
		comboBox.setSelectedItem(value);
			}

	    public Object getCellEditorValue() {
		return comboBox.getSelectedItem();
	    }
				
			boolean shouldSelectCell(EventObject anEvent) { 
				if (anEvent instanceof MouseEvent) { 
					MouseEvent e = (MouseEvent)anEvent;
					return e.getID() != MouseEvent.MOUSE_DRAGGED;
				}
				return true;
			}
		};
	comboBox.addActionListener(delegate);
	}
public DimCellEditor(final UIRefPane refTextField) {
    editorComponent = refTextField;
    final nc.ui.pub.beans.UITextField textField = refTextField.getUITextField();
    this.clickCountToStart = 2;
    delegate = new EditorDelegate() {
        public void setValue(Object value) {
            refTextField.setValue((value != null) ? value.toString() : "");
            //textField.setText((value != null) ? value.toString() : "");
        }
        public Object getCellEditorValue() {
            if (refTextField.isReturnCode()) //|| !refTextField.isButtonVisible())
                return refTextField.getText();
            //return textField.getText();
            else
                return refTextField.getRefName();
        }
        public void setPK(Object value) {
            refTextField.setPK((value != null) ? value.toString() : "");
        }
    };
    textField.addActionListener(delegate);
}
//
//  Constructors
//

	/**
	 * Constructs a DefaultCellEditor that uses a text field.
	 *
	 * @param x  a JTextField object ...
	 */
	public DimCellEditor(final UITextField textField) {
		editorComponent = textField;
	this.clickCountToStart = 2;
		delegate = new EditorDelegate() {
			public void setValue(Object value) {
		textField.setText((value != null) ? value.toString() : "");
			}

	    public Object getCellEditorValue() {
		return textField.getText();
	    }
		};
	textField.addActionListener(delegate);
	}
//
//  Handle the event listener bookkeeping
//
	// implements javax.swing.CellEditor
	public void addCellEditorListener(CellEditorListener l) {
	listenerList.add(CellEditorListener.class, l);
	}
	// implements javax.swing.CellEditor
	public void cancelCellEditing() {
	fireEditingCanceled();
	}
	/*
	 * Notify all listeners that have registered interest for
	 * notification on this event type.  The event instance 
	 * is lazily created using the parameters passed into 
	 * the fire method.
	 * @see EventListenerList
	 */
	protected void fireEditingCanceled() {
	// Guaranteed to return a non-null array
	Object[] listeners = listenerList.getListenerList();
	// Process the listeners last to first, notifying
	// those that are interested in this event
	for (int i = listeners.length-2; i>=0; i-=2) {
	    if (listeners[i]==CellEditorListener.class) {
		// Lazily create the event:
		if (changeEvent == null)
		    changeEvent = new ChangeEvent(this);
		((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
	    }	       
	}
	}
	/*
	 * Notify all listeners that have registered interest for
	 * notification on this event type.  The event instance 
	 * is lazily created using the parameters passed into 
	 * the fire method.
	 * @see EventListenerList
	 */
	protected void fireEditingStopped() {
	// Guaranteed to return a non-null array
	Object[] listeners = listenerList.getListenerList();
	// Process the listeners last to first, notifying
	// those that are interested in this event
	for (int i = listeners.length-2; i>=0; i-=2) {
	    if (listeners[i]==CellEditorListener.class) {
		// Lazily create the event:
		if (changeEvent == null)
		    changeEvent = new ChangeEvent(this);
		((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
	    }	       
	}
	}
//
//  Implementing the CellEditor Interface
//

	// implements javax.swing.CellEditor
	public Object getCellEditorValue() {
		return delegate.getCellEditorValue();
	}
	/**
	 *  ClickCountToStart controls the number of clicks required to start
	 *  editing.
	 */
	public int getClickCountToStart() {
	return clickCountToStart;
	}
	/**
	 * Returns the a reference to the editor component.
	 *
	 * @return the editor Component
	 */
	public Component getComponent() {
	return editorComponent;
	}
//
//  Implementing the CellEditor Interface
//

// implements javax.swing.table.TableCellEditor
public Component getTableCellEditorComponent(
	JTable table, 
	Object value, 
	boolean isSelected, 
	int row, 
	int column) {
	
	//if (table.getModel() instanceof CellListTableModel){
		//CellListTableModel model = (CellListTableModel)table.getModel();
		//ColumnData coldata = model.getColdata()[column];//model.getBodyColByCol(column)];
		//if (item.getDataType() == BillItem.UFREF){
			//if (item.getIDColName() != null && !item.getIDColName().equals("")){
				//Object id = model.getValueAt(row,item.getIDColName());
				////if (id != null)
					////delegate.setPK(id.toString());
				//delegate.setPK(id);
			//}
		//}else{
			//delegate.setValue(value);
		//}
	//}else{
		delegate.setValue(value);
	//}
	return editorComponent;
}
//
//  Implementing the TreeCellEditor Interface
//

	// implements javax.swing.tree.TreeCellEditor
	public Component getTreeCellEditorComponent(JTree tree, Object value,
						boolean isSelected,
						boolean expanded,
						boolean leaf, int row) {
	String         stringValue = tree.convertValueToText(value, isSelected,
					    expanded, leaf, row, false);

	delegate.setValue(stringValue);
	return editorComponent;
	}
	// implements javax.swing.CellEditor
	public boolean isCellEditable(EventObject anEvent) {
		if (anEvent instanceof MouseEvent) { 
			return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
		}
		return true;
	}
	// implements javax.swing.CellEditor
	public void removeCellEditorListener(CellEditorListener l) {
	listenerList.remove(CellEditorListener.class, l);
	}
//
//  Modifying
//

	/**
	 * Specifies the number of clicks needed to start editing.
	 *
	 * @param count  an int specifying the number of clicks needed to start editing
	 * @see #getClickCountToStart
	 */
	public void setClickCountToStart(int count) {
	clickCountToStart = count;
	}
	// implements javax.swing.CellEditor
	public boolean shouldSelectCell(EventObject anEvent) { 
	return delegate.shouldSelectCell(anEvent); 
	}
	// implements javax.swing.CellEditor
	public boolean stopCellEditing() {
	fireEditingStopped();
		return true;
	}
} // End of class JCellEditor
